<template>
  <rx-layout>
    <div slot="center">
      <a-card :title="i18n('list.comment1','文件存储配置')" >
        <div style="text-align: right;padding-bottom: 10px;">
          <a-button type="primary" @click="handleSave">{{$ti18('rxForm.saveBtn','保存')}}</a-button>
        </div>
      <a-form-model ref="form" :model="mdl" :rules="rules" >
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" :label="i18n('form.fileSystem','文件服务类型')" prop="fileSystem">
              <a-radio-group v-model="fileSystem" @change="defChangeType">
                <a-radio v-for="item in sysFileStoreTypeList"
                         :value="item.value"
                         :key="item.value">
                  {{item.label}}
                </a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <component ref="compomentsValid"
                       :dataObj="dataObj"
                       :is="currentComponet">
            </component>
          </a-col>
        </a-row>
      </a-form-model>
      </a-card>
    </div>
  </rx-layout>
</template>
<script>
  import SysFileStoreApi from '@/api/system/core/sysFileStore'
  import {RxLayout, BaseFormModel} from "@lowcode/jpaas-common-lib";


  import SysFileStoreDefault from './defComponents/SysFileStoreDefault';
  import SysFileStorefile from './defComponents/SysFileStorefile';
  import SysFileStorefastdfs from './defComponents/SysFileStorefastdfs';
  import SysFileStorealiyunOss from './defComponents/SysFileStorealiyunOss';
  import SysFileStorelangchaoOss from './defComponents/SysFileStorelangchaoOss';
  import SysFileStoreminio from './defComponents/SysFileStoreminio';
  import SysFileStoreTenyunCos from './defComponents/SysFileStoreTenyunCos';
  import SysFileStores3 from './defComponents/SysFileStores3';
  import defaultConfig from "./defComponents/defaultConfig"

  export default {
    name: 'SysFileStoreEdit',
    mixins: [BaseFormModel],
    components: {
      RxLayout,
      SysFileStorefile,
      SysFileStorefastdfs,
      SysFileStorealiyunOss,
      SysFileStorelangchaoOss,
      SysFileStoreminio,
        SysFileStores3,
      "SysFileStoretenyunCos":SysFileStoreTenyunCos
    },
    data() {
      return {
          labelCol: { span: 3 },
          wrapperCol: { span: 21 },
        rules: {
          fileSystem: [{required: false, message: this.i18n('form.fileSystem','文件服务类型','select'), trigger: 'change'}],
        },
        dataObj:{
        },
        currentComponet: SysFileStoreDefault,
        fileSystem: "",
        sysFileStoreTypeList: [],
      }

    },
    created() {
      this.getSysFileStoreTypeList();
    },
    methods: {
      i18n(name,text,key){
          return this.$ti18(name,text,"SysFileStore",key);
      },
      defChangeType() {
        if(!this.fileSystem){
            this.fileSystem="file";
        }
        this.dataObj=defaultConfig[this.fileSystem];
        this.currentComponet = "SysFileStore" + this.fileSystem;
      },
      getSysFileStoreTypeList() {
        var self_ = this;
        SysFileStoreApi.getSysFileStoreTypeList().then(res => {
          var list = res.data;
          for (var key in list) {
            var temp = list[key];
              self_.sysFileStoreTypeList.push({label: temp, value: key});
          }
          self_.getSysFileStoreNacosConfig();
        });
      },
      getSysFileStoreNacosConfig(){
        var self_ = this;
        SysFileStoreApi.getSysFileStoreNacosConfig().then(res => {
          if(res.data){
            let config =(typeof res.data == 'object')?res.data:JSON.parse(res.data);
            if(config.fileSystem){
                self_.fileSystem=config.fileSystem;
                self_.dataObj = config;
                self_.currentComponet = "SysFileStore" + self_.fileSystem;
            }else{
                self_.fileSystem="file";
                self_.defChangeType();
            }
          }else{
              self_.fileSystem="file";
              self_.defChangeType();
          }
        });
      },
      handleSave(){
        this.dataObj.fileSystem=this.fileSystem;
        SysFileStoreApi.saveStore(this.dataObj);
      },
    }
  }
</script>
<style>
  .relick a {
    margin-right: 10px;
  }

</style>